home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / news / inn1.000 / inn1.4sec-linux-src.tar / inn / syslog / logger.c < prev    next >
C/C++ Source or Header  |  1992-07-24  |  4KB  |  196 lines

  1. /*  $Revision: 1.4 $
  2. **  Modified by Rich $alz <rsalz@osf.org> to be more portable to older
  3. **  systems.
  4. */
  5. /*
  6.  * Copyright (c) 1983 Regents of the University of California.
  7.  * All rights reserved.
  8.  *
  9.  * Redistribution and use in source and binary forms are permitted
  10.  * provided that: (1) source distributions retain this entire copyright
  11.  * notice and comment, and (2) distributions including binaries display
  12.  * the following acknowledgement:  ``This product includes software
  13.  * developed by the University of California, Berkeley and its contributors''
  14.  * in the documentation or other materials provided with the distribution
  15.  * and in all advertising materials mentioning features or use of this
  16.  * software. Neither the name of the University nor the names of its
  17.  * contributors may be used to endorse or promote products derived
  18.  * from this software without specific prior written permission.
  19.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  20.  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  21.  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  22.  */
  23.  
  24. #ifndef lint
  25. char copyright[] =
  26. "@(#) Copyright (c) 1983 Regents of the University of California.\n\
  27.  All rights reserved.\n";
  28. #endif /* not lint */
  29.  
  30. #ifndef lint
  31. static char sccsid[] = "@(#)logger.c    6.14 (Berkeley) 6/1/90";
  32. #endif /* not lint */
  33.  
  34. #include <stdio.h>
  35. #define SYSLOG_NAMES
  36. #include "syslog.h"
  37. #include <ctype.h>
  38.  
  39. /*
  40. **  LOGGER -- read and log utility
  41. **
  42. **    This routine reads from an input and arranges to write the
  43. **    result on the system log, along with a useful tag.
  44. */
  45.  
  46. /*
  47. **  Return a string representation of errno.
  48. */
  49. static char *
  50. xstrerror(e)
  51.     int        e;
  52. {
  53.     extern int    sys_nerr;
  54.     extern char    *sys_errlist[];
  55.     static char    buff[30];
  56.  
  57.     if (e >= 0 && e < sys_nerr)
  58.     return sys_errlist[e];
  59.     (void)sprintf(buff, "Error code %d\n", e);
  60.     return buff;
  61. }
  62.  
  63. main(argc, argv)
  64.     int argc;
  65.     char **argv;
  66. {
  67.     extern char *optarg;
  68.     extern int errno, optind;
  69.     int pri = LOG_NOTICE;
  70.     int ch, logflags = 0;
  71.     char *tag, buf[1024], *getlogin();
  72.  
  73.     tag = NULL;
  74.     while ((ch = getopt(argc, argv, "f:ip:st:")) != EOF)
  75.         switch((char)ch) {
  76.         case 'f':        /* file to log */
  77.             if (freopen(optarg, "r", stdin) == NULL) {
  78.                 (void)fprintf(stderr, "logger: %s: %s.\n",
  79.                     optarg, xstrerror(errno));
  80.                 exit(1);
  81.             }
  82.             break;
  83.         case 'i':        /* log process id also */
  84.             logflags |= LOG_PID;
  85.             break;
  86.         case 'p':        /* priority */
  87.             pri = pencode(optarg);
  88.             break;
  89.         case 's':        /* log to standard error */
  90.             logflags |= LOG_PERROR;
  91.             break;
  92.         case 't':        /* tag */
  93.             tag = optarg;
  94.             break;
  95.         case '?':
  96.         default:
  97.             usage();
  98.         }
  99.     argc -= optind;
  100.     argv += optind;
  101.  
  102.     /* setup for logging */
  103.     openlog(tag ? tag : getlogin(), logflags, 0);
  104.     (void) fclose(stdout);
  105.  
  106.     /* log input line if appropriate */
  107.     if (argc > 0) {
  108.         register char *p, *endp;
  109.         int len;
  110.  
  111.         for (p = buf, endp = buf + sizeof(buf) - 2; *argv;) {
  112.             len = strlen(*argv);
  113.             if (p + len > endp && p > buf) {
  114.                 syslog(pri, "%s", buf);
  115.                 p = buf;
  116.             }
  117.             if (len > sizeof(buf) - 1)
  118.                 syslog(pri, "%s", *argv++);
  119.             else {
  120.                 if (p != buf)
  121.                     *p++ = ' ';
  122.                 bcopy(*argv++, p, len);
  123.                 *(p += len) = '\0';
  124.             }
  125.         }
  126.         if (p != buf)
  127.             syslog(pri, "%s", buf);
  128.         exit(0);
  129.     }
  130.  
  131.     /* main loop */
  132.     while (fgets(buf, sizeof(buf), stdin) != NULL)
  133.         syslog(pri, "%s", buf);
  134.  
  135.     exit(0);
  136. }
  137.  
  138.  
  139. /*
  140.  *  Decode a symbolic name to a numeric value
  141.  */
  142. pencode(s)
  143.     register char *s;
  144. {
  145.     char *save;
  146.     int fac, lev;
  147.  
  148.     for (save = s; *s && *s != '.'; ++s);
  149.     if (*s) {
  150.         *s = '\0';
  151.         fac = decode(save, facilitynames);
  152.         if (fac < 0)
  153.             bailout("unknown facility name: ", save);
  154.         *s++ = '.';
  155.     }
  156.     else {
  157.         fac = 0;
  158.         s = save;
  159.     }
  160.     lev = decode(s, prioritynames);
  161.     if (lev < 0)
  162.         bailout("unknown priority name: ", save);
  163.     return ((lev & LOG_PRIMASK) | (fac & LOG_FACMASK));
  164. }
  165.  
  166.  
  167. decode(name, codetab)
  168.     char *name;
  169.     CODE *codetab;
  170. {
  171.     register CODE *c;
  172.  
  173.     if (isdigit(*name))
  174.         return (atoi(name));
  175.  
  176.     for (c = codetab; c->c_name; c++)
  177.         if (!strcasecmp(name, c->c_name))
  178.             return (c->c_val);
  179.  
  180.     return (-1);
  181. }
  182.  
  183. bailout(msg, arg)
  184.     char *msg, *arg;
  185. {
  186.     fprintf(stderr, "logger: %s%s\n", msg, arg);
  187.     exit(1);
  188. }
  189.  
  190. usage()
  191. {
  192.     fputs("logger: [-i] [-f file] [-p pri] [-t tag] [ message ... ]\n",
  193.         stderr);
  194.     exit(1);
  195. }
  196.